package com.study.demoaopacl.aspects;

import com.alibaba.fastjson.JSONObject;
import com.study.demoaopacl.anno.ValidResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.CodeSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @Name: AccCheckAspect
 * @Description: java类作用描述
 * @Author: yn
 * @CreateDate: 2022/7/1 16:17
 * @Remark: 特殊说明
 * @Version: 1.0
 */
@Aspect
@Configuration
public class AccCheckAspect {

    @Pointcut("@annotation(com.study.demoaopacl.anno.AccCheck)")
    public void aclPointcut(){
    }

    @Around("aclPointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Map<String, ParamInfo> nameAndValue = getNameAndValue(joinPoint);

        Object[] args = joinPoint.getArgs();
        JSONObject source = new JSONObject();
        source.put("name","123");

        MethodSignature joinPointObject = (MethodSignature) joinPoint.getSignature();
        Method method = joinPointObject.getMethod();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        for (int i = 0; i < parameterAnnotations.length; i++) {
            System.out.println(parameterAnnotations);
            for (Annotation annotation : parameterAnnotations[i]) {
                if(annotation instanceof ValidResult){
                    args[i]=JSONObject.parseObject(source.toJSONString(),((ValidResult) annotation).value());
                }
            }
        }
        return joinPoint.proceed(args);
    }


    private static Map<String, ParamInfo> getNameAndValue(ProceedingJoinPoint joinPoint) {
        Map<String, ParamInfo> param = new HashMap<>();
        MethodSignature joinPointObject = (MethodSignature) joinPoint.getSignature();
        Method method = joinPointObject.getMethod();

        String[] paramNames = joinPointObject.getParameterNames();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        Object[] paramValues = joinPoint.getArgs();

        for (int i = 0; i < paramNames.length; i++) {
            ParamInfo paramInfo = new ParamInfo();
            paramInfo.setName(paramNames[i]);
            paramInfo.setValue(paramValues[i]);
            paramInfo.setAnnotations(parameterAnnotations[i]);
            param.put(paramNames[i],paramInfo);
        }
        return param;
    }


//    private static Map<String, Object> getNameAndValue(ProceedingJoinPoint joinPoint) {
//        Map<String, Object> param = new HashMap<>();
//        Object[] paramValues = joinPoint.getArgs();
//        String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
//        for (int i = 0; i < paramNames.length; i++) {
//            param.put(paramNames[i], paramValues[i]);
//        }
//        return param;
//    }
}
